package com.woniu.woniuk16.controller;

import com.woniu.woniuk16.model.Result;
import com.woniu.woniuk16.param.AddScore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

@RestController
@RequestMapping("/redis")
public class RedisTestController {
    @Autowired
    private RedisTemplate<String,Object> rtl;

    @PostMapping("/addScore")
    public Result add(@RequestBody AddScore addScore){
        rtl.opsForZSet().add("woniu:class:score:"+addScore.getClassUserId(),addScore,addScore.getScoreScore());
        return Result.getInstance(null);
    }

    @GetMapping("/getScores/{id}")
    public Result getRedisS(@PathVariable Integer id){
        Set<Object> objects = rtl.opsForZSet().rangeByScore("woniu:class:score:" + id, 0, -1);
        return Result.getInstance(objects);
    }
}
